/** Refresh data from spreadsheets */
function refreshSourceData(year, nextMethod) {
    loadCompetitors(year, function(){
        loadCheckPointData(year, function(){
            nextMethod();
        });
    });
}

/** Worksheet indexes of years. 
  2011 - Worksheet 1
  2010 - Worksheet 2
  2009 - Worksheet 3
*/
function getWorksheetIndex(year) {
    if (year=="2011") return 1 ;
    else if (year=="2010") return 2 ;
    else return 3 ;
}

function calculateStatistics(){
    var retval = {};
    retval.checkPointCount = checkPoints.length;
    retval.competitorCount = competitors.length;
    retval.measurementCount = 0;
    retval.competitorCountByCategory = {};
    retval.arrivedCountByCategory = {};
    retval.arrivedCount = 0;
    $.each(checkPoints, function() {
        if (isFinite(this.measurementCount)) {
            retval.measurementCount += this.measurementCount;
        }
    });
    
    //Sorting the competitors in the competitorsByCategory to reflect the race results
    function compareCompetitors(a, b) {
        var aDist = a.lastCheckPoint.totalDistances[a.category];
        var bDist = b.lastCheckPoint.totalDistances[b.category];
        if (aDist < bDist || (aDist == bDist && a.lastMeasurementTime > b.lastMeasurementTime)) {
            return 1;
        }
        return -1;
    }
    $.each(competitorsByCategory, function() {
        this.sort(compareCompetitors);
        if (this.length > 0) {
            retval.competitorCountByCategory[this[0].category] = this.length;
            var cat = this[0].category;
            if (typeof(retval.arrivedCountByCategory[cat]) === 'undefined') {
                retval.arrivedCountByCategory[cat] = 0;
            }
            $.each(this, function() {
                if (this.lastCheckPoint == checkPoints[checkPoints.length - 1]) {
                    retval.arrivedCountByCategory[cat] += 1;
                    retval.arrivedCount += 1;
                }
            });
        }
    });
    
    retval.totalDistance = 0;
    $.each(competitors, function() {
        retval.totalDistance += this.lastCheckPoint.totalDistances[this.category];
    });
    return retval;
}
var stats = null;

function initialize(){
    document.getElementById("diagramYearSelector").selectedIndex = getWorksheetIndex('2011') - 1 ;
    document.getElementById("tableYearSelector").selectedIndex = getWorksheetIndex('2011') - 1;
    document.getElementById("chartCategorySelector").value = '115' ;
    document.getElementById("tableCategorySelector").value = '115' ;    
    tableCategory = '115' ;
    chartCategory = '115' ;
    document.getElementById("tableDataTypeSelector").value = 'relative' ;    
    tableDataType = 'relative' ;
    chartDataType = 'relative' ;
    tableCompletion = 'N' ;
    chartCompletion = 'N' ;
    document.getElementById("chartCompletionSelector").value = 'N' ;
    document.getElementById("tableCompletionSelector").value = 'N' ;    
    
    refreshSourceData('2011', function(){
        //refreshStatistics();
        setTimeout(refreshTable, 100);
        $('#tabs').bind('tabsselect', function(event, ui) {
            //if (ui.index == 1) {
                setTimeout(refreshChart, 300);
            //}
            //else if (ui.index == 2) {
            //    setTimeout(initMap, 100);
           //}
        });
        setTimeout(refreshChart, 300);
    });
}

function refresh() {
    setYear(document.getElementById('tableYearSelector').value);
}

function setYear(year) {
    document.getElementById("diagramYearSelector").value = year ;//.selectedIndex = getWorksheetIndex(year) - 1 ;
    document.getElementById("tableYearSelector").value = year ;
    $("#loading").fadeIn(1500);    
    refreshSourceData(year, function(){
        //refreshStatistics();
        //if ( $('#tabs').tabs().tabs('option', 'selected') == 1 ) {
            refreshChart() ;
        //}        
        refreshTable();
    });
}

function setCategory(category) {
    document.getElementById("tableCategorySelector").value = category ;
    document.getElementById("chartCategorySelector").value = category ;
    tableCategory = category ;
    refreshTable() ;
    chartCategory = category ;
    //if ( $('#tabs').tabs().tabs('option', 'selected') == 1 ) {
        refreshChart() ;
    //}
}

function setCompletion(completion) {
    document.getElementById("chartCompletionSelector").value = completion ;
    document.getElementById("tableCompletionSelector").value = completion ;
    tableCompletion = completion ;
    chartCompletion = completion ;
    refreshTable() ;
    //if ( $('#tabs').tabs().tabs('option', 'selected') == 1 ) {
        refreshChart() ;
    //}
}

function setDataType(dataType) {
    document.getElementById("tableDataTypeSelector").value = dataType ;
    document.getElementById("chartDataTypeSelector").value = dataType ;
    tableDataType = dataType ;
    chartDataType = dataType ;
    //if ( $('#tabs').tabs().tabs('option', 'selected') == 1 ) {
        refreshChart() ;
    //}            
    refreshTable() ;
}



function openDialog(id){ 
    $("#" + id).dialog( "option", "title", 'Leírás' );
    $("#" + id).dialog('open');       
}

function showDiagram() {
        document.getElementById('diagramtab').style.display = "" ;
        document.getElementById('tabletab').style.display = "none" ;
} 

function showTable() {
        document.getElementById('diagramtab').style.display = "none" ;
        document.getElementById('tabletab').style.display = "" ;
}

